package com.xiaojianone.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojianone.pojo.Headline;
import com.xiaojianone.pojo.vo.PortalVo;
import com.xiaojianone.service.HeadlineService;
import com.xiaojianone.mapper.HeadlineMapper;
import com.xiaojianone.utils.JwtHelper;
import com.xiaojianone.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 徐国健
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-02-18 14:18:47
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private HeadlineMapper headlineMapper;

    /**
     * 首页数据查询
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {

        IPage<Map> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());

        headlineMapper.selectMyPage(page,portalVo);

        List<Map> records = page.getRecords();

        Map pageInfoMap = new HashMap();

        pageInfoMap.put("pageData",records);
        pageInfoMap.put("pageNum",page.getCurrent());
        pageInfoMap.put("pageSize",page.getSize());
        pageInfoMap.put("totalPage",page.getPages());
        pageInfoMap.put("totalSize",page.getTotal());

        Map dataMap = new HashMap();
        dataMap.put("pageInfo",pageInfoMap);

        return Result.ok(dataMap);
    }


    /**
     * 根据id查询详情
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {

        Map headlineMap = headlineMapper.queryDetailMap(hid);

        Map dataMap = new HashMap();

        dataMap.put("headline",headlineMap);

        //修改阅读量 +1
        Headline headline = new Headline();
        headline.setHid((Integer) headlineMap.get("hid"));
        headline.setVersion((Integer) headlineMap.get("version"));

        headline.setPageViews((Integer) headlineMap.get("pageViews") +1 );
        headlineMapper.updateById(headline);

        return Result.ok(dataMap);
    }


    /**
     * 发布头条方法
     * @return
     */
    @Override
    public Result publish(Headline headline,String token) {

        //根据token查询用户id
        int userId = jwtHelper.getUserId(token).intValue();

        //数据装配
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());

        headlineMapper.insert(headline);

        return Result.ok(null);
    }


    /**
     * 头条修改数据
     * @param headline
     * @return
     */
    @Override
    public Result updateDate(Headline headline) {

        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();

        headline.setVersion(version);
        headline.setUpdateTime(new Date());

        headlineMapper.updateById(headline);

        return Result.ok(null);
    }
}




